/*
 * Copyright (c) 2012 by Fernando Correa da Conceição
 *
 * This is released under MIT license, read the file LICENSE.txt or
 * http://opensource.org/licenses/mit-license.php .
 *
 * Some parts of this file are generated from form editor in netbeans.
 */
package info.jaguaribe.jaguaribe;

import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Administrador
 */
public class PreferencesFrame extends javax.swing.JFrame {
    /**
     * Used for class serialization.
     */
    private static final long serialVersionUID = 1L;

    /**
     * The model used by table.
     */
    private DefaultTableModel model = null;

    /**
     * Creates new form PreferencesFrame
     */
    public PreferencesFrame() {
        initComponents();
        ResourceBundle msg = Jaguaribe.getInstance().getMessages();

        this.model = new DefaultTableModel() {
            public boolean isCellEditable(int row, int column) {
                return false;//This causes all cells to be not editable
            }
        };
        this.model.addColumn(msg.getString("PreferencesFrame.Keyboard.Table.Column.Name"));
        this.model.addColumn(msg.getString("PreferencesFrame.Keyboard.Table.Column.Current"));
        this.model.addColumn(msg.getString("PreferencesFrame.Keyboard.Table.Column.Default"));
        this.model.addColumn("Key");
        this.tableKeyboard.setModel(this.model);
        this.tableKeyboard.removeColumn(tableKeyboard.getColumnModel().getColumn(3));
        this.tableKeyboard.revalidate();

        // Fill actions in the keyboard table
        HashMap<String, KeyboardAction> actions = UserPreferences.getInstance().keyboard.actions;
        for (Map.Entry<String, KeyboardAction> entry : actions.entrySet()) {
            String key = entry.getKey();
            KeyboardAction action = entry.getValue();
            this.model.addRow(new Object[]{action.getDescription(),
                action.getKeyStroke(), action.getDefaultValue(), key});
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        panelKeyboard = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableKeyboard = new javax.swing.JTable();
        buttonChange = new javax.swing.JButton();
        buttonResetDefault = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        buttonOk = new javax.swing.JButton();
        buttonCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tableKeyboard.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(tableKeyboard);

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("info/jaguaribe/jaguaribe/Bundle"); // NOI18N
        buttonChange.setText(bundle.getString("PreferencesFrame.buttonChange.text")); // NOI18N
        buttonChange.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonChangeActionPerformed(evt);
            }
        });

        buttonResetDefault.setText(bundle.getString("PreferencesFrame.buttonResetDefault.text")); // NOI18N
        buttonResetDefault.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonResetDefaultActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelKeyboardLayout = new javax.swing.GroupLayout(panelKeyboard);
        panelKeyboard.setLayout(panelKeyboardLayout);
        panelKeyboardLayout.setHorizontalGroup(
            panelKeyboardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelKeyboardLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 324, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(panelKeyboardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(buttonChange)
                    .addComponent(buttonResetDefault))
                .addGap(37, 37, 37))
        );
        panelKeyboardLayout.setVerticalGroup(
            panelKeyboardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelKeyboardLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(panelKeyboardLayout.createSequentialGroup()
                .addGap(77, 77, 77)
                .addComponent(buttonChange)
                .addGap(27, 27, 27)
                .addComponent(buttonResetDefault)
                .addContainerGap(219, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab(bundle.getString("PreferencesFrame.panelKeyboard.TabConstraints.tabTitle"), panelKeyboard); // NOI18N
        panelKeyboard.getAccessibleContext().setAccessibleName(bundle.getString("PreferencesFrame.panelKeyboard.AccessibleContext.accessibleName")); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 493, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 375, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab(bundle.getString("PreferencesFrame.jPanel1.TabConstraints.tabTitle"), jPanel1); // NOI18N

        buttonOk.setText(bundle.getString("PreferencesFrame.buttonOk.text")); // NOI18N
        buttonOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonOkActionPerformed(evt);
            }
        });

        buttonCancel.setText(bundle.getString("PreferencesFrame.buttonCancel.text")); // NOI18N
        buttonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(84, 84, 84)
                .addComponent(buttonOk)
                .addGap(68, 68, 68)
                .addComponent(buttonCancel)
                .addContainerGap())
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING)
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {buttonCancel, buttonOk});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 409, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonOk)
                    .addComponent(buttonCancel))
                .addGap(0, 20, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Event. When the button change is clicked.
     *
     * @param evt The event.
     */
    private void buttonChangeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonChangeActionPerformed
        int line = this.tableKeyboard.convertRowIndexToModel(this.tableKeyboard.getSelectedRow());
        if (line > -1) {
            System.out.println(tableKeyboard.getModel().getValueAt(line, 3));
            KeyboardPressDialog dialog = new KeyboardPressDialog(this, true);
            dialog.setVisible(true);
            if (dialog.status) {
                tableKeyboard.getModel().setValueAt(dialog.getKeystroke(), line, 1);
                this.tableKeyboard.revalidate();
            }
        }
    }//GEN-LAST:event_buttonChangeActionPerformed

    /**
     * Event. When the button reset is clicked.
     *
     * @param evt The event.
     */
    private void buttonResetDefaultActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonResetDefaultActionPerformed
        int line = this.tableKeyboard.convertRowIndexToModel(this.tableKeyboard.getSelectedRow());
        if (line > -1) {
            tableKeyboard.getModel().setValueAt(tableKeyboard.getModel().getValueAt(line, 2), line, 1);
            this.tableKeyboard.revalidate();
        }
    }//GEN-LAST:event_buttonResetDefaultActionPerformed

    /**
     * Event. When the button ok is clicked.
     *
     * @param evt The event.
     */
    private void buttonOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonOkActionPerformed
        HashMap<String, KeyboardAction> actions = UserPreferences.getInstance().keyboard.actions;
        //Get value from the keyboard options
        for (int i = 0; i < this.model.getRowCount(); i++) {
            String key = tableKeyboard.getModel().getValueAt(i, 3).toString();
            String value = tableKeyboard.getModel().getValueAt(i, 1).toString();
            KeyboardAction action = actions.get(key);
            action.setKeyStroke(value);
            actions.put(key, action);
        }
        UserPreferences.getInstance().save();
        this.dispose();
    }//GEN-LAST:event_buttonOkActionPerformed

    /**
     * Event. When the button cancel is clicked.
     *
     * @todo Ask if the user really want to close without save the changes.
     * @param evt The event.
     */
    private void buttonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCancelActionPerformed
        this.dispose();
    }//GEN-LAST:event_buttonCancelActionPerformed
    //@cond
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonCancel;
    private javax.swing.JButton buttonChange;
    private javax.swing.JButton buttonOk;
    private javax.swing.JButton buttonResetDefault;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JPanel panelKeyboard;
    private javax.swing.JTable tableKeyboard;
    // End of variables declaration//GEN-END:variables
    //@endcond
}
